package cn.ywang.spring.framework.aop.framework.adapter;

import cn.ywang.spring.framework.aop.aspect.AbstractAspectAdvice;
import cn.ywang.spring.framework.aop.aspect.Advice;
import cn.ywang.spring.framework.aop.intercept.Joinpoint;
import cn.ywang.spring.framework.aop.intercept.MethodInterceptor;
import cn.ywang.spring.framework.aop.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * @author BeatificWang
 * @date 2019/5/4 21:32
 */
public class AfterReturningAdviceInterceptor extends AbstractAspectAdvice implements Advice, MethodInterceptor {

	/**
	 * Non-null if after returning advice binds the return value
	 */
	private String returningName;

	private Joinpoint joinpoint;

	public AfterReturningAdviceInterceptor(Method method, Object o) {
		super(method, o);
	}

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		Object retVal = mi.proceed();
		this.joinpoint = mi;
		this.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
		return retVal;
	}

	private void afterReturning(Object retVal, Method method, Object[] arguments, Object aThis) throws Throwable {
		super.invokeAdviceMethod(this.joinpoint, retVal, null);
	}
}
